public class Leet394 {
    public String decodeString(String s) {
        String[] a = dfs(0,s);
        return a[1];
    }
    static String[] dfs(int at, String s){
        StringBuilder sb = new StringBuilder();
        String  num = "";
        while (at<s.length()){
            if (s.charAt(at)<='9'&&s.charAt(at)>='0'){
                num+=s.charAt(at);
                at++;
                continue;
            }
            if (s.charAt(at)=='['){
                at++;
                String[] temp = dfs(at, s);
                if (!num.equals(""))
                    for (int i = 0; i < Integer.parseInt(num); i++) {
                        sb.append(temp[1]);
                    }
                at = Integer.parseInt(temp[0]);
                num = "";
                continue;
            }
            if(s.charAt(at)==']'){
                return new String[]{String.valueOf(at+1), sb.toString()};
            }
            sb.append(s.charAt(at));
            at++;
        }
        return new String[]{"",sb.toString()};
    }


}
